package com.ericsson.rm.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.ericsson.rm.form.ProjectDetailCommand;
import com.ericsson.rm.service.ProjectDetailService;
import com.ericsson.rm.util.Utility;
import com.ericsson.rm.validator.ProjectDetailValidator;
import com.ericsson.rm.vo.ProjectDetailsVO;

@Controller
public class ProjectDetailsController {
	
	  private final static Logger logger = LoggerFactory.getLogger(ProjectDetailsController.class);
	  private final static String CLASS_NAME = ProjectDetailsController.class.getSimpleName();
	  private final static String PAGE_INSERT_PROJECTDETAIL_FORM = "projectDetail";
	  
	  @Autowired
	  ProjectDetailService projectDetailService ;
	  
	  private ProjectDetailValidator validator ;

	public ProjectDetailService getProjectDetailService() {
		return projectDetailService;
	}

	public void setProjectDetailService(ProjectDetailService projectDetailService) {
		this.projectDetailService = projectDetailService;
	}
	
	@Autowired
	public ProjectDetailsController(ProjectDetailValidator  projValidator){
		if(logger.isDebugEnabled()){
		      logger.debug(CLASS_NAME+" constructor entering...");
		  }
		this.validator = projValidator;   
	}
	  
	  @RequestMapping("ProjectDetails")
	  public String initForm(ModelMap model)
	  {
	    final String METHOD_NAME = CLASS_NAME + ".initForm() ";
	    System.out.println("Init method......................................");
	    if(logger.isDebugEnabled()){
	      logger.debug(METHOD_NAME+"entering...");
	    } 
	 
	    ProjectDetailCommand  projectDetailCommand = new ProjectDetailCommand(); 
	    model.addAttribute("ProjectDetailCommand",projectDetailCommand);
	    return PAGE_INSERT_PROJECTDETAIL_FORM;
	  }
	  
	  @InitBinder
	  public void initBinder(WebDataBinder binder) {
	    final String METHOD_NAME = CLASS_NAME + ".initBinder() ";
	    if(logger.isDebugEnabled()){
	      logger.debug(METHOD_NAME+"entering...");
	    }
	    
	    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	  }
	  
	  @RequestMapping("/dosubmit")
	  public String getProjectDetails(@ModelAttribute ("ProjectDetailCommand") ProjectDetailCommand projectDetailCmd , BindingResult result ,SessionStatus status){
		  
		   final String METHOD_NAME = CLASS_NAME + "getProjectDetails() ";
		     
	        if(logger.isDebugEnabled()){
	            logger.debug(METHOD_NAME+"entering...");
	            logger.debug(METHOD_NAME+"ProjectDetailCommand: "+Utility.displayBean(projectDetailCmd));
	        }
	        
	        if(result.hasErrors())
	           {
	             return " ";
	           }
	        else{
	        	
	        ProjectDetailsVO project_details = (ProjectDetailsVO)Utility.copyBean(projectDetailCmd,ProjectDetailsVO.class);	
	        projectDetailService.saveProjectDetails(project_details);
	        System.out.println("after service call");
	        	
	        }
		 
		  return "Success";
	  }
	  
	

}
